Problem Statement
Demonstrate two tasks using ASP.NET:
Task 1: Develop an ASP.NET page that displays a list of options with check boxes (use CheckBoxList
web control). On clicking a button (web control), the page displays the selected options in a label control.
Task 2: Develop an ASP.NET page that displays two text boxes and a button web control. The textboxes are used to capture number of rows and columns from user.
Code Implementation
Task 1: CheckBoxList
CheckListTest.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="CheckListTest.aspx.vb" Inherits="CheckBoxTest" %>
<html>
<head runat="server">
<title>CheckBoxTest</title>
</head>
<body>
<form method="post" runat="server">
Choose your favorite programming languages:<br /><br />
<asp:CheckBoxList id="chklst" runat="server" /><br /><br />
<asp:Button id="cmdOK" Text="OK" runat="server" />
<br /><br />
<asp:Label id="lblResult" runat="server" />
</form>
</body>
</html>
CheckListTest.aspx.vb
Partial Public Class CheckBoxTest
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Me.IsPostBack = False Then
chklst.Items.Add("C")
chklst.Items.Add("C++")
chklst.Items.Add("C#")
chklst.Items.Add("Visual Basic 6.0")
chklst.Items.Add("VB.NET")
chklst.Items.Add("Pascal")
End If
End Sub
Protected Sub cmdOK_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdOK.Click
lblResult.Text = "You chose:<b>"
For Each lstItem As ListItem In chklst.Items
If lstItem.Selected Then
lblResult.Text &= "<br />" & lstItem.Text
End If
Next
lblResult.Text &= "</b>"
End Sub
End Class
Task 2: Table Control
TableTest.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="TableTest.aspx.vb" Inherits="TableTest" %>
<html>
<head runat="server">
<title>Table Test</title>
</head>
<body>
<form method="post" runat="server">
Rows:
<asp:TextBox id="txtRows" runat="server" />
Cols:
<asp:TextBox id="txtCols" runat="server" /><br /><br />
<asp:CheckBox id="chkBorder" runat="server" Text="Put Border Around Cells" />
<br /><br />
<asp:Button id="cmdCreate" runat="server" Text="Create" />
<br /><br />
<asp:Table id="tbl" runat="server" />
</form>
</body>
</html>
TableTest.aspx.vb
Partial Public Class TableTest
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
tbl.BorderStyle = BorderStyle.Inset
tbl.BorderWidth = Unit.Pixel(1)
End Sub
Protected Sub cmdCreate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdCreate.Click
tbl.Controls.Clear()
Dim i, j As Integer
For i = 0 To Val(txtRows.Text) - 1
Dim rowNew As New TableRow()
tbl.Controls.Add(rowNew)
For j = 0 To Val(txtCols.Text) - 1
Dim cellNew As New TableCell()
cellNew.Text = "Cell (" & i.ToString() & "," & j.ToString() & ") 😀"
If chkBorder.Checked Then
cellNew.BorderStyle = BorderStyle.Inset
cellNew.BorderWidth = Unit.Pixel(1)
End If
rowNew.Controls.Add(cellNew)
Next
Next
End Sub
End Class